package synapsolution;

import java.text.DecimalFormat;
import java.util.Random;

public class Synapse {

    static final double learningRate = .4;
    static final double maxWeight = 1;
    static final double minWeight = -1;
    private double weight;
    private Neuron output;
    final ThreadLocal<Random> rng = new ThreadLocal<Random>();

    public Synapse(Neuron output) {
        this.output = output;
        rng.set(new Random());
        weight = randomWeight();
    }

    public void send() {
        output.activate(weight);
    }

    public void combine(Synapse a, Synapse b) {
        //weight = (a.weight + b.weight) / 2;
        weight = (rng.get().nextBoolean()) ? (a.weight + b.weight) / 2 : (rng.get().nextBoolean()) ? a.weight : b.weight;
    }

    public void mutate() {
        weight = randomWeight();
    }

    @Override
    public String toString() {
        String value = new DecimalFormat("#0.0000").format(weight);
        while (value.length() < 7) value = " " + value;
        return value;
    }

    private double randomWeight() {
        return rng.get().nextDouble() * 2 - 1;
    }
}
